<template>
  <el-row type="flex" justify="space-between" align="middle" style="height:40px; width: 100%;">
    <el-col>
      <span>{{ treeNode.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <el-col>
          <span style="font-size:14px;">{{ treeNode.manager }}</span>
        </el-col>
        <el-col>
          <el-dropdown @command="handleDelete">
            <span> 操作<i class="el-icon-arrow-down" /></span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="DEPARTS_ADD">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="DEPARTS_EDIT">编辑部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="DEPARTS_DEL">删除部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartments } from '@/api/departments'
import { DEPARTS_ADD, DEPARTS_DEL, DEPARTS_EDIT } from '@/constants'

export default {
  name: 'TreeTooles',
  props: {
    treeNode: {
      type: Object,
      required: true
    },
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      DEPARTS_ADD,
      DEPARTS_DEL,
      DEPARTS_EDIT
    }
  },
  methods: {
    async handleDelete(tag) {
      if (tag === this.DEPARTS_ADD) {
        // 添加
        this.$emit('addDepts', this.treeNode)
      } else if (tag === this.DEPARTS_EDIT) {
        // 修改
        this.$emit('editDepts', this.treeNode)
      } else {
        // 删除
        try {
          await this.$confirm('此操作将永久删除, 是否继续?', '提示', {
            type: 'warning'
          })
          await delDepartments(this.treeNode.id)
          this.$emit('delDepts')
          this.$message.success('删除成功')
        } catch (error) {
          if (error === 'cancel') { this.$message('已取消删除') }
        }
      }
    }
  }
}
</script>

<style>

</style>
